<?php

require_once './include/common.inc.php';

$page_title = 'jans2en furniture - Products - Products';
$menu_id = 5;

$sql = "SELECT `id`, `fid`, `name`, `pic`, `description` FROM `category` ORDER BY `seq` ASC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	if ($rs['fid']==0) {
		$out_ary[$rs['id']] = $rs;
	} else {
		$sub_category_ary[$rs['fid']][] = $rs['id'];
	}
}



// вƷid
$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` GROUP BY c.`fid`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$fid_ary[] = $rs['fid'];   //ͼƬ
}

$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p, `product` pr WHERE pr.`id`=p.`product_id` AND c.`id`=p.`category_id` AND c.`fid` = 0 AND pr.`hidden_flag` = 0  GROUP BY p.`category_id`";
//$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` AND c.`fid` = 0 GROUP BY p.`category_id`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$fid2_ary[] = $rs['category_id'];   //ͼƬ
}
// sqlfid=0ʱεһЩֻиĲƷֻʣһ
foreach ($out_ary as $k=>$v) {
	if (!isset($sub_category_ary[$k]) && !in_array($k, $fid2_ary)) {
		unset($out_ary[$k]);
	}
}
//echo "<pre>";print_r($out_ary);
// -------------- guideͼƬ start ----------------------------------------
$system_ary = getSystemValue("");
$product_description = nl2br($system_ary['productset']);
$guide_pic = $system_ary['picture_products'];
if (trim($guide_pic) == "" || !file_exists("./attachments/system/" . $guide_pic)) {
	$guide_pic = './attachments/system/no_guide.gif';
} else {
	$guide_pic = './attachments/system/' . $guide_pic;
}
// -------------- guideͼƬ end ------------------------------------------

// ѡһŲƷͼƬ,ûвƷ
if (!empty($out_ary)) {
	foreach ($out_ary as $k1=>$v1) {
		$sql = "SELECT p.`small_pic`, p.`id` FROM `product` p, `product_category` c where p.id=c.product_id AND c.category_id = $k1 AND p.`small_pic` != '' AND p.`hidden_flag` = 0 ORDER BY RAND( ) limit 1 ";
		$tmp_ary = $db->get_one($sql);
		
		if (empty($tmp_ary) && !key_exists($k1, $sub_category_ary)) {
			//unset($out_ary[$k1]);   // ɾֻиp.`hidden_flag` = 0ĲƷ࣬ע⣺ʱ `small_pic` != ''
		} else {
		$while_start = 1;
		$while_end = 20;

		while ($while_start<=$while_end && (trim($tmp_ary['small_pic']) == "" || !file_exists("./attachments/" . $tmp_ary['small_pic']))) {
			$tmp_ary = $db->get_one($sql);
			$while_start++;
		}

		$out_ary[$k1]['pic'] = @$tmp_ary['small_pic'];
		}

	}



}

if (!empty($sub_category_ary)) {
	foreach ($sub_category_ary as $k=>$v) {

		$temp_str = implode(",", $v);
		/*
		if (!empty($temp_str)) {
		$temp_str = $k . "," . $temp_str;   // ֱڸӽȥ
		}*/
		$sql = "SELECT p.`small_pic`, p.`id` FROM `product` p, `product_category` c where p.id=c.product_id AND c.category_id IN (" . $temp_str . ") AND p.`small_pic` != '' AND p.`hidden_flag` = 0 ORDER BY RAND( ) limit 1 ";
		//echo $sql;
		$tmp_ary = $db->get_one($sql);

		$while_start = 1;
		$while_end = 20;

		while ($while_start<=$while_end && (trim($tmp_ary['small_pic']) == "" || !file_exists("./attachments/" . $tmp_ary['small_pic']))) {
			$tmp_ary = $db->get_one($sql);
			$while_start++;
		}

		if (!in_array($k, $fid_ary)) {
			unset($out_ary[$k]);
		} else {
			$out_ary[$k]['pic'] = @$tmp_ary['small_pic'];
		}

	}
}

if ($HAS_NEW_ITEM && $jansen_type > 0 && $jansen_uid > 0) {

	$sql = "SELECT `small_pic` FROM `product` where `small_pic` != '' AND `new_flag` = 1 ORDER BY RAND( ) limit 1 ";
	$tmp_ary = $db->get_one($sql);

	$out_ary[] = array(
	'id' => 1001,
	'name' => 'New Items',
	//'pic' => 'new_items.jpg',
	'pic' => $tmp_ary['small_pic'],
	'seq' => '1001',
	//'description' => getSystemValue('newitemset'),
	'description' => $system_ary['newitemset']
	);
}

//echo "<pre>";print_r($out_ary);exit;

/*$out_ary[] = array(
'id' => 1002,
'name' => 'Specials',
'pic' => 'specials.jpg',
'seq' => '1002',
'description' => 'Jans2en furniture has recently placed the following items on sale. A limited amount of inventory is available on a first come, first served basis. Our sales are a result of discontinued inventory, over stocked items, or occasionally we may just want to su'
);*/
//echo "<pre>";print_r($out_ary);exit;

	browseTracking(18,0,'Home > Product');
	
require_once PrintEot('header');
require_once PrintEot('products');
require_once PrintEot('footer');

?>